<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
    <button onclick="get()">get请求</button>
    <button onclick="post()">post请求</button>
    <script>
        function get() {
            // axios简化形式
            let params = {
                name: 'zs',
                age: 18
            }
            axios.get('http://127.0.0.1/api/get', {
                params
            })
                .then(function (response) {
                    // 处理成功情况
                    console.log(response);
                })
                .catch(function (error) {
                    // 处理错误情况
                    console.log(error);
                })
        }

        function post() {
            // axios一般形式
            axios({
                url: 'http://127.0.0.1/api/post',
                method: 'POST',
                data: {
                    "age": 12
                },
                headers: {
                    //'Content-Type': 'application/x-www-form-urlencoded'=> name1=value1&name2=value2
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then(function (res) {
                console.log(res.data);
            });
        }
    </script>
</body>

</html>